package com.truthbean.jeetb.utils.web.invoker;

import com.truthbean.jeetb.utils.web.DBOperator;
import com.truthbean.jeetb.utils.web.OnLineProcess;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.ServerSocket;

public class LogoutInvoker extends DefaultInvoker {
    public boolean doBefore() {
        return false;
    }

    public boolean doAfter() {
        return false;
    }

    public void invoke(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
        try {
            Object dbo = session.getAttribute(DBO);
            if (dbo != null)
                ((DBOperator) dbo).close();
            Object obj = session.getAttribute(PORT_MAP);
            if (obj != null) {
                ServerSocket s = (ServerSocket) obj;
                s.close();
            }
            Object online = session.getAttribute(SHELL_ONLINE);
            if (online != null)
                ((OnLineProcess) online).stop();
            session.invalidate();
            response.sendRedirect(SHELL_NAME + "?o=vLogin");
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}